<?php
namespace Admin\Controller;
use Common\Controller\BaseController;

class AdminController extends BaseController {

	public function _initialize(){

    	parent::_initialize();

    	// 用户权限检查
		if (C ( 'USER_AUTH_ON' ) && !in_array(MODULE_NAME,explode(',',C('NOT_AUTH_MODULE')))) {
			$rbac = \Org\Util\Rbac::AccessDecision();			
			if (!$rbac) {	
				//检查认证识别号
				if (! $_SESSION [C ( 'USER_AUTH_KEY' )]) {
					//跳转到认证网关
					redirect ( C ( 'USER_AUTH_GATEWAY' ) );
				}
				// 没有权限 抛出错误
				if (C ( 'RBAC_ERROR_PAGE' )) {
					// 定义权限错误页面
					redirect ( C ( 'RBAC_ERROR_PAGE' ) );
				} else {
					if (C ( 'GUEST_AUTH_ON' )) {
						$this->assign ( 'jumpUrl', C ( 'USER_AUTH_GATEWAY' ) );
					}
					// 提示错误信息
					$this->error ( L ( '_VALID_ACCESS_' ) );
				}
			}
		}

		$this->adminID = $_COOKIE['adminID'];		
		$this->adminName = $_COOKIE['adminName'];
		$this->adminGroup = M('Role')->where('id='.$_COOKIE['adminGroup'])->getField('name');

		$this->assign('adminName',$this->adminName);
		$this->assign('adminGroup',$this->adminGroup);  		
    }

    //保存配置文件
	protected function update_config($config, $config_file = '') {
		!is_file($config_file) && $config_file = CONF_PATH . 'web.php';
		if (is_writable($config_file)) {
			file_put_contents($config_file, "<?php \nreturn " . stripslashes(var_export($config, true)) . ";", LOCK_EX);
			@unlink(RUNTIME_FILE);
			return true;
		} else {
			return false;
		}
	}
    
}